package jsyf.oa.dao.wreport;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.oa.entity.wreport.WReportCustomerEntity;
import jsyf.oa.idao.BaseDao;
import jsyf.oa.ientity.IBaseEntity;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: @张巨波 @zhangjubo
 * @creat: on 2017/11/24.
 * @since: V1.0.0
 * @see jsyf.oa.web
 */
@Repository
public class WReportCustomerDao extends BaseDao {
    public  WReportCustomerDao(){
        this.iBaseEntity = new WReportCustomerEntity();
    }
    public List<WReportCustomerEntity> getEntityListByCondition(String condition, Object[] params) {
        List<JSONObject> jsonList = getJSONListByCondition(condition, params);
        List<WReportCustomerEntity> entityList = new ArrayList<>();
        for (int i = 0; i < jsonList.size(); i++) {
            WReportCustomerEntity entity = (WReportCustomerEntity) convertJson2Entity(jsonList.get(i));
            entityList.add(entity);
        }
        return entityList;
    }
    @Override
    public IBaseEntity convertJson2Entity(JSONObject json) {
        return JSON.toJavaObject(json, WReportCustomerEntity.class);
    }
    public List<JSONObject> getCustomerType(){
        StringBuffer sb = new StringBuffer();
        sb.append(" SELECT \n ");
        sb.append(" ID AS id ,\n ");
        sb.append(" CUSTOMER_TYPE AS customerType \n ");
        sb.append(" FROM  \n ");
        sb.append(" t_wrt_customer_type\n ");
        List<JSONObject> jsonList = super.queryForJsonList(sb.toString());
        return jsonList;
    };

    public WReportCustomerEntity getByTel(String tel){
        List<IBaseEntity> list = this.getIBaseEntityByCondition(" AND TEL = ? ",new String[]{tel});
        if(list != null && list.size()>0) {
            return (WReportCustomerEntity) list.get(0);
        }else{
            return  null;
        }
    }
    public WReportCustomerEntity getById(String id){
        List<JSONObject> list = this.getJSONListByCondition(" and id = ?",new String[]{id});
        if ( list != null && list.size() > 0 ){
            return JSON.toJavaObject(list.get(0),WReportCustomerEntity.class);
        } else {
            return null;
        }
    }
}
